import socket


def start_client():
    # 创建客户端套接字
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # 连接服务器
    host = '218.91.114.181'
    port = 49999

    try:
        client_socket.connect((host, port))
        print("已连接到服务器")

        while True:
            # 发送消息
            message = input("请输入要发送的消息 (输入 'quit' 退出): ")
            if message.lower() == 'quit':
                break

            client_socket.send(message.encode('utf-8'))

            # 接收响应
            response = client_socket.recv(1024).decode('utf-8')
            print(f"服务器响应: {response}")

    except Exception as e:
        print(f"发生错误: {e}")
    finally:
        client_socket.close()


if __name__ == "__main__":
    start_client()